window.app2web_mode = false;
console.log(">>>> waitting for Html5plus arrive");
document.addEventListener("plusready", function() {
	console.log("<<<< Html5plus initized!");
	var muiCss = "https://dfc643.gitee.io/xwlink-mapps/common/mui.css";
	var muiJs = "https://dfc643.gitee.io/xwlink-mapps/common/mui.js";
	var head  = document.getElementsByTagName('head')[0];
	console.log(">>>> start loading basic stylesheet");
	var link  = document.createElement('link');
	link.rel  = 'stylesheet';
	link.type = 'text/css';
	link.href = muiCss;
	head.appendChild(link);
	console.log("<<<< stylesheet loaded!");
	console.log(">>>> start initizing ui elements");
	var script = document.createElement('script');
	script.onload = function () {
		mui.init();
		document.body.style.opacity = 1;
		console.log("<<<< ui library loaded!");
		if (typeof mAppInit === "function") {
			console.log(">>>> calling mAppInit() function");
			mAppInit.call(mui);
		}
	};
	script.src = muiJs;
	document.head.appendChild(script);
}, false);

//__loading__
function xwLoading(e, o) {
	if (e && mui('#xwLoading').length === 0) {
		var elem = document.createElement('div');
		elem.innerHTML = `
		<div id="xwLoading" style="position:fixed;z-index:999;top:0;left:0;right:0;bottom:0;">
			<div style="position:fixed;width:60vw;height:20vh;background:rgba(0,0,0,0.8);border-radius:10px;top:50%;left:50%;margin-left:-30vw;margin-top:-12.5vh;color:#fff;text-align:center;font-size:0.9em;line-height:150%;padding:1.2em;box-shadow: 0 0 10em #555;">
				<i class="mui-spinner mui-spinner-white" style="width:4em;height:4em;margin-top:0.8em;"></i><br/>
				请稍候
			</div>
		</div>`;
		document.body.appendChild(elem);
		var $m = mui('.xw-blur');
		for(i=0; i<$m.length; i++) {
			$m[i].style.filter = "blur(5px)";
		}
		o.call(this);
	} else if (e === false) {
		var $e = mui('#xwLoading');
		if ($e.length > 0) {
			$e[0].remove();
		}
		var $m = mui('.xw-blur');
		for(i=0; i<$m.length; i++) {
			$m[i].style.filter = "unset";
		}
		o.call(this);
	}
}

xwNullObj = function() {};
function xwlink_get(cmdpath, succ, err) {
	mui.plusReady(function () {
	   var xw_uri = plus.storage.getItem('xw_uri');
	   var xw_user = plus.storage.getItem('xw_user');
	   var xw_pwd = plus.storage.getItem('xw_pwd');
	   mui.ajax(`${xw_uri}/${cmdpath}`, {
	   	dataType: 'html',
	   	type: 'get',
	   	headers: {'Authorization':'Basic '+btoa(`${xw_user}:${xw_pwd}`)},
	   	success: function(data) {
	   		succ.call(this, data);
	   	},
	   	error: function(data) {
	   		err.call(this, data);
	   	}
	   }); 
	})
}


function xwlink_post(cmdpath, data, succ, err) {
	mui.plusReady(function () {
	    var xw_uri = plus.storage.getItem('xw_uri');
	    var xw_user = plus.storage.getItem('xw_user');
	    var xw_pwd = plus.storage.getItem('xw_pwd');
	    //__serialize_data_without_%2B_
	    serialize = function(obj) {
	        var str = [];
	        for (var p in obj)
	        if (obj.hasOwnProperty(p)) {
	            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
	        }
	        return str.join("&");
	    }
	    mui.ajax(`${xw_uri}/${cmdpath}`, {
	    	data: data,
	    	dataType: 'html',
	    	type: 'post',
	    	//processData: false,
	    	headers: {
	    		'Authorization':'Basic '+btoa(`${xw_user}:${xw_pwd}`),
	    		'Content-Type':'application/x-www-form-urlencoded'
	    	},
	    	success: function(data) {
	    		succ.call(this, data);
	    	},
	    	error: function(data) {
	    		err.call(this, data);
	    	}
	    });
	})
}